package etr.bk.gui.rpan.info;

import etr.bk.gui.*;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.sql.Connection;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTextArea;
import javax.swing.border.Border;
import javax.swing.plaf.SeparatorUI;

import org.h2.jdbc.JdbcSQLException;

import etr.bk.db.ConnectDB;
import etr.bk.db.WorkDBInfo;
import etr.bk.db.WrapperRPanInfo;

public class InfoPanel extends JPanel {
	private BufferedImage img;
	private WorkDBInfo obj;
	private TextInfoPanel infoPan;
	private PortraitPane fotka;
	private boolean flagForFotka;

	private JTextArea textArea;

	private MainListener mListener;

	public InfoPanel() {
		// --------------------------Picture--------------------
		this.fotka = new PortraitPane(this.img);
		fotka.setPreferredSize(new Dimension(200, 280));
		fotka.setBackground(Color.DARK_GRAY);
		fotka.setBorder(BorderFactory.createRaisedBevelBorder());

		this.add(fotka);

		this.flagForFotka = false;

		FlowLayout flowLay = new FlowLayout(FlowLayout.CENTER, 7, 5);
		this.setLayout(flowLay);

		// --------------Text info PAnel-----------------

		// TextInfoPanel textPan = new TextInfoPanel();

		this.infoPan = new TextInfoPanel();
		infoPan.setPreferredSize(new Dimension(345, 280));
		this.add(infoPan);
		infoPan.setAllFieldsInactive();

		// -------------Text ARea For notes---------------

		textArea = new JTextArea();
		textArea.setLineWrap(true);
		textArea.setDisabledTextColor(Color.BLACK);
		JScrollPane scrollPanel = new JScrollPane(textArea);
		scrollPanel
				.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		scrollPanel.setPreferredSize(new Dimension(550, 180));
		Border border = BorderFactory.createRaisedBevelBorder();
		scrollPanel.setBackground(Color.GRAY);
		scrollPanel.setBorder(BorderFactory.createTitledBorder(border,
				"For notes"));

		textArea.setEnabled(false);

		this.add(scrollPanel);

		// -------------------------ADD
		// Listener---------------------------------------

		this.mListener = MainListener.getListener();
		this.mListener.setInfPanel(this);
		this.fotka.addMouseListener(mListener);
	}

	public void clearAllFields() {
		this.textArea.setText("");
		this.infoPan.clearAllFields();

		// PictureFromfFile p = new PictureFromfFile(new File("D:/noname.bmp"));
		// this.img = p.getImageFromFile();

		this.fotka.setImage(null);// delllllllllllllllllllllllllllllllllllllllllll
		this.fotka.repaint();
		// this.fotka.;

	}

	public BufferedImage setFotoFile(File f) {
		if (f != null) {
			PictureFromfFile p = new PictureFromfFile(f);
			this.img = p.getImageFromFile();
		} else {
			this.img = null;
			System.out.println("null in info panel for fotka");
		}
		this.fotka.setImage(img);
		this.fotka.repaint();
		return img;
	}

	public void setAllFieldsInactive() {
		this.infoPan.setAllFieldsInactive();
		this.textArea.setEnabled(false);

		this.flagForFotka = false;
		// need flag for picture panel inactive (listener not work)
	}

	public void setAllFieldsActive() {
		this.infoPan.setAllFieldsActive();
		this.textArea.setEnabled(true);

		this.flagForFotka = true;
		// need flag for picture panel inactive (listener not work)
	}

	// --------------------SETTER / GETTER--------------------------
	public String getNotes() {
		String str = this.textArea.getText();

		if (str.equals("")) {
			System.out.println("know where is problem");
			return null;
		} 
		else {
			// System.out.println("*****"+str);
			return str;
		}
	}

	private void setNotes(String str) {
		this.textArea.setText(str);
	}

	public BufferedImage getImage() {
		return this.fotka.getImg();
	}

	private void setImage(BufferedImage image) {
		this.img = image;
		this.fotka.setImage(image);
		this.fotka.repaint();
	}

	public ArrayList<String> getInfo() {
		return this.infoPan.getInfo();

		// NULL if mandatory fields isn't fill
	}

	private void setInfo(ArrayList<String> arrStr) {
		this.infoPan.setInfo(arrStr);
	}

	public void setAllInfo(WrapperRPanInfo wrap) {
		if (wrap != null) {
			this.setNotes(wrap.getNotes());
			this.setInfo(wrap.getArrayItem());
			this.setImage(wrap.getImg());

			this.repaint();
		}
	}

	public boolean getFlagForFotka() {
		return flagForFotka;
	}

}
